package com.kaishengit.web;
 
import java.io.*;  
import java.util.UUID;

import javax.servlet.*;  
import javax.servlet.http.*;  

import com.kaishengit.entity.Contact;
import com.kaishengit.service.ContactService;
import com.kaishengit.util.ZxingEncoderHandler;
import com.sun.image.codec.jpeg.*;//sun鍏徃浠呮彁渚涗簡jpg鍥剧墖鏂囦欢鐨勭紪鐮乤pi  
import javax.imageio.stream.*;  
import java.awt.*;  
import java.awt.image.BufferedImage;  
public class ShowPicture  extends HttpServlet {  
     /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String GIF="image/gif;charset=GB2312";//璁惧畾杈撳嚭鐨勭被鍨� 
     private static final String JPG="image/jpeg;charset=GB2312";  
     public void init()  throws ServletException  
     {  
     }  
     public void doGet(HttpServletRequest request, HttpServletResponse response)  
                  throws IOException, ServletException  
     {  
        doPost(request, response);  
     }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
                 throws IOException, ServletException  
   {  
          
    	
    	
    	
      /*  String spec=request.getParameter("spec");//杈撳嚭鍥剧墖鐨勭被鍨嬬殑鏍囧織  
        int int_spec=Integer.parseInt(spec);  
        String imagePath ;
        if(int_spec==1)      
        {  
        	imagePath="c:/b1.jpg";//鍥剧墖鐩稿web搴旂敤鐨勪綅缃� 
        }  
        else  
        {  
             imagePath="c:/b2.gif";//鍥剧墖鐩稿web搴旂敤鐨勪綅缃� 
        }      */
          
       //寰楀埌杈撳嚭娴� 
   /*  if(imagePath.toLowerCase().endsWith(".jpg"))//浣跨敤缂栫爜澶勭悊鏂囦欢娴佺殑鎯呭喌锛� 
      {  
     response.setContentType(JPG);//璁惧畾杈撳嚭鐨勭被鍨� 
          //寰楀埌鍥剧墖鐨勭湡瀹炶矾寰�       
          imagePath = getServletContext().getRealPath(imagePath);  
         //寰楀埌鍥剧墖鐨勬枃浠舵祦  
          InputStream imageIn = new FileInputStream(new File(imagePath));  
         //寰楀埌杈撳叆鐨勭紪鐮佸櫒锛屽皢鏂囦欢娴佽繘琛宩pg鏍煎紡缂栫爜  
     JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);  
         //寰楀埌缂栫爜鍚庣殑鍥剧墖瀵硅薄  
         BufferedImage image = decoder.decodeAsBufferedImage();  
         //寰楀埌杈撳嚭鐨勭紪鐮佸櫒  
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);  
          encoder.encode(image);//瀵瑰浘鐗囪繘琛岃緭鍑虹紪鐮� 
          imageIn.close();//鍏抽棴鏂囦欢娴� 
       }  */
      /* if(imagePath.toLowerCase().endsWith(".gif"))//涓嶄娇鐢ㄧ紪鐮佸鐞嗘枃浠舵祦鐨勬儏鍐碉細  
      {  
    	   OutputStream os = response.getOutputStream();
    	   
		   response.setContentType(GIF);  
		  
		   InputStream is = new FileInputStream(imagePath);
		   BufferedInputStream bis=new BufferedInputStream(is);//杈撳叆缂撳啿娴� 
		   BufferedOutputStream bos=new BufferedOutputStream(os);//杈撳嚭缂撳啿娴� 
		   byte data[]=new byte[4096];//缂撳啿瀛楄妭鏁� 
		   int size=0;   
		   size=bis.read(data);  
		   while (size!=-1)  
		   {  
		       os.write(data,0,size);  
		       size=is.read(data);  
		    }  
		    bis.close();
		    bos.flush();//娓呯┖杈撳嚭缂撳啿娴� 
		    bos.close(); 
    
   
    
    }  */
       
       String id = request.getParameter("id");
       ContactService service = new ContactService();
       Contact contact = service.findContact(id);
       if(contact != null){
    	   OutputStream os = response.getOutputStream();
           ZxingEncoderHandler handel = new ZxingEncoderHandler();
           handel.encodeByStream("名字"+contact.getName()+"地址"+contact.getAddress()+"公司"+contact.getCompany()+"手机"+contact.getCellphone()+"邮箱"+contact.getEmail(), 100, 100, os);
    	   
       }
      // InputStream is = new FileInputStream(new File("c:/"+id+"jpg"));
      
     
     
       
              
     }  
}  
